package me.firstapp.service.bbs.impl;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.Page;
import me.firstapp.module.bbs.MemberPointAccountTrace;
import me.firstapp.module.bbs.PointAccount;
import me.firstapp.repository.bbs.MemberPointAccountTraceRepository;
import me.firstapp.repository.bbs.PointAccountRepository;
import me.firstapp.service.bbs.MemberPointAccountTraceService;

@Service
public class MemberPointAccountTraceServiceImpl implements MemberPointAccountTraceService {

	@Autowired
	private PointAccountRepository pointAccountRepository;

	@Autowired
	private MemberPointAccountTraceRepository memberPointAccountTraceRepository;

	@Override
	public Page<MemberPointAccountTrace> findMemberPointAccountTracePage(Integer pageNo, Integer pageSize,
			Long memberId, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户id");
		}
		PointAccount pointAccount = pointAccountRepository.findByMemberId(memberId, apiClientId);
		if (pointAccount == null) {
			throw new ServiceException(StatusHouse.POINT_ACCOUNT_NOT_EXIST);
		}
		return memberPointAccountTraceRepository.findPointAccountTracePage(pageNo, pageSize, pointAccount.getId(),
				apiClientId);
	}

	@Override
	public Page<MemberPointAccountTrace> findPointAccountTracePageByParams(Long memberId, Integer pageNo,
			Integer pageSize, Map<String, Object> searchParams, String sortParam, String sortType, Date startTime,
			Date endTime, Long apiClientId) {
		PointAccount pointAccount = pointAccountRepository.findByMemberId(memberId, apiClientId);
		if (pointAccount == null) {
			throw new ServiceException(StatusHouse.POINT_ACCOUNT_NOT_EXIST);
		}

		return memberPointAccountTraceRepository.findPointAccountTracePageByParams(pointAccount.getId(), pageNo,
				pageSize, searchParams, sortParam, sortType, startTime, endTime, apiClientId);
	}

}
